/**
 * @file    bt_timer.c
 * @author  chenwenrui
 * @brief   定时器模块
 * @version V1.0
 * @date 2023-12-19
 * @copyright Copyright (c) 2022
 *
 * 系统资源：
 *      timer0:
 *          TIMER0_VECTOR
 *
 */

#include "timer.h"
#include "bt_timer.h"
#include "bt_task.h"

//static bt_timer_callback _timer = 0;

/*
void Timer0_Init(void)		//10毫秒@22.1184MHz
{
    AUXR &= 0x7F;			//定时器时钟12T模式
    TMOD &= 0xF0;			//设置定时器模式
    TL0 = 0x00;				//设置定时初始值
    TH0 = 0xB8;				//设置定时初始值
    TF0 = 0;				//清除TF0标志
    TR0 = 1;				//定时器0开始计时
}

*/
/**
 * @brief  定时器初始化:1ms
 * @param  n_ms : 定时时长
 * @note
 * @retval
 */
void bt_timer_init(void)
{
    TIM_InitTypeDef xdata TIM_InitStructure;                    // 结构定义
    TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;           // 指定工作模式,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
    TIM_InitStructure.TIM_Polity = Polity_0;                    // 指定中断优先级(低到高) Polity_0,Polity_1,Polity_2,Polity_3
    TIM_InitStructure.TIM_Interrupt = ENABLE;                   // 中断是否允许,   ENABLE或DISABLE
    TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;             // 指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
    TIM_InitStructure.TIM_ClkOut = DISABLE;                     // 是否输出高速脉冲, ENABLE或DISABLE
                                                                // TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000); // 中断频率, 1000次/秒
    TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000); // 中断频率, 1000次/秒
    TIM_InitStructure.TIM_Run = ENABLE;                         // 是否初始化后启动定时器, ENABLE或DISABLE
    Timer_Inilize(Timer0, &TIM_InitStructure);                  // 初始化Timer0	  Timer0,Timer1,Timer2,Timer3,Timer4
}

/**
 * @brief  定时器注册回调函数
 * @param  cb : 定时回调函数
 * @note
 * @retval
 */
//void bt_timer_register(bt_timer_callback cb)
//{
//    _timer = cb;
//}

/**
 * @brief  定时器中断处理，,10ms回调
 * @note   TIMER0_VECTOR
 * @retval
 */
void timer0_int(void) interrupt TIMER0_VECTOR
{
    static unsigned char cnt = 0;
    if (cnt++ >= 10)
    {
        cnt = 0;
        bt_timer_loop();
    }
}